var express = require('express');
var router = express.Router();

var formController = require('../controllers/form')

var path = require('path')
var ueditor = require("ueditor")
router.use("/ue", ueditor(path.join(__dirname, '../../public'), function(req, res, next) {

	// ueditor 客户发起上传图片请求
  
	if(req.query.action === 'uploadimage'){
  
	  // 这里你可以获得上传图片的信息
	  var foo = req.ueditor;
	  console.log(foo.filename); // exp.png
	  console.log(foo.encoding); // 7bit
	  console.log(foo.mimetype); // image/png
  
	  // 下面填写你要把图片保存到的路径 （ 以 path.join(__dirname, 'public') 作为根路径）
	  var img_url = '/uploads';
	  res.ue_up(img_url); //你只要输入要保存的地址 。保存操作交给ueditor来做
	}
	//  客户端发起图片列表请求
	else if (req.query.action === 'listimage'){
	  var dir_url = '/uploads'; // 要展示给客户端的文件夹路径
	  res.ue_list(dir_url) // 客户端会列出 dir_url 目录下的所有图片
	}
	// 客户端发起其它请求
	else {
  
	  res.setHeader('Content-Type', 'application/json');
	  // 这里填写 ueditor.config.json 这个文件的路径
	  res.json(require('./config'))
  }}));


router.get('/', async (req, res, next) => {
	res.redirect('/fd/forms')
})

router.get('/forms', async (req, res, next) => {
	console.log('get forms');
	let forms = await formController.getFormList();

	res.render('forms', {forms} )
})

router.get('/form_data_list', async (req, res, next) => {
	console.log('get form data list');
	let form = await formController.getForm(req.query.id);
	let formDataList = await formController.getFormDataList(req.query.id);

	res.render('form_data_list', {form, data: formDataList} )
})

router.get('/data_edit', async (req, res, next) => {
	console.log('show form data');
	let form = await formController.getForm(req.query.form_id);
	let formData = {data: {}};
	if (req.query.data_id) {
		formData = await formController.getFormData(req.query.data_id);
	}
	let { design_content } = formController.getFormEdit(form, formData.data, 'edit');

	res.render('data_edit', {form, formData, design_content})
})

router.get('/data_view', async (req, res, next) => {
	console.log('show form data');
	let form = await formController.getForm(req.query.form_id);
	let formData = {data: {}};
	if (req.query.data_id) {
		formData = await formController.getFormData(req.query.data_id);
	}
	let { design_content } = formController.getFormEdit(form, formData.data, 'view');

	res.render('data_view', {form, formData, design_content})
})

router.post('/data_edit', async (req, res, next) => {
	console.log('submit form data');
	let form = await formController.getForm(req.body.form_id);
	await formController.saveFormData(form, req.body);
	//res.redirect('form_data_list?id=req.body.form_id')
	res.redirect('/fd/form_data_list?id='+form.id)
})

router.get('/form_preview', async (req, res, next) => {
	console.log('show form preview');
	let formPreview = await formController.getFormPreview(req.query.id);
	res.render('form_preview', {...formPreview})
})

router.get('/form_design', async (req, res, next) => {
	console.log('show design form');
	let form = await formController.getForm(req.query.id);
	res.render('form_design', {form})
})

router.get('/form_design_embed', async (req, res, next) => {
	console.log('show embed design form');
	let form = await formController.getForm(req.query.id);
	res.render('form_design_embed', {form})
})

router.post('/form_design', (req, res, next) => {
	console.log('save form content');
	formController.saveDesign(req.body.form_id, req.body.design_content)
	res.json({status: 1})
})

router.get('/form_edit', async (req, res, next) => {
	console.log('show edit form');
	let form = {};
	if (req.query.id) {
		form = await formController.getForm(req.query.id);
	}
	res.render('form_edit', {form})
})

router.post('/form_edit', async (req, res, next) => {
	console.log('save form data');
	await formController.saveForm(req.body);
	res.redirect('/fd/forms')
})

router.post('/saveFormContent', async (req, res, next) => {
	console.log('save form_parse');
	await formController.saveNewDesign(req.body);
	res.json({status: 1})
})

router.post('/upload', async (req, res, next) => {
	var multer = require('multer');
	var storage = multer.diskStorage({
		destination: function (req, file, cb) {
			cb(null, './public/uploads')
		},
		filename: function (req, file, cb) {
			var fileFormat = (file.originalname).split(".");
			// cb(null, file.fieldname + '-' + Date.now() + "." + fileFormat[fileFormat.length - 1]);
			cb(null,`${file.fieldname}-${Date.now()}${Math.floor((Math.random()*10000)+1)}.${fileFormat[fileFormat.length - 1]}`)
		}
	});
	var upload = multer({
		storage: storage
	});
	var upload = upload.array('file', 500)
	upload(req, res, function (err) {
		if (err) {
			return console.log(err);
		}
		var finalPaths = req.files.map((file) => {
			return { path: `/uploads/${file.filename}` }
		})
		res.send(finalPaths);
		//res.send({ path: `/uploads/${req.file.filename}` })
	});
})

module.exports = router